<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Sms as Smslib;
use app\common\library\Token;
use think\captcha\Captcha;
use think\Db;
use think\Request;
use think\Validate;
use app\index\model\Apply;


class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function home()
    {
        return $this->view->fetch();
    }
    public function supermarket()
    {
        return $this->view->fetch();
    }
    public function personal()
    {
        return $this->view->fetch();
    }
    public function index(Request $request)
    {

        $ip = $request->ip();
        $this->assign('ip',$ip);
        return $this->view->fetch();
    }

    public function reg2(Request $request){

        if ($request->isPost()){
            $User = input('user/a',[]);
            $res = $this->validate($User,'User.regist1');

            if ($res !== true){
                $this->success($res);exit;
            };
            return $this->view->fetch('info',$User);
        }
        return;
    }

    public function info(Request $request){
        $res = input('info/a');

        if ($request->isPost()){
            $res2 = $this->validate($res,'User.user');

            if ($res2 !== true){
                $this->error($res2);
            };

            $user = model('apply');

            $ret = $user->getByTel($res['tel']);


            $r = [
                'update_time'=>time(),
                'ip' => $request->ip(),
                'is_reg'=>Db::raw('is_reg+1')
            ];

            $r = array_merge($res,$r);

            // 如果已经注册过了
            if ( $ret ){
                $res3 = $user->allowField(true)->isUpdate(true)->save($r,['id'=>$ret->id]);

                if ( $res3 ){
                    $user->bet($ret->tel);
                    $this->redirect('achieve');
                    exit;
                }else{
                    $this->error($res3);
                }
            }else{
                $res['is_reg'] = 0;
                $res['update_time'] = time();
                $res['ip'] = $request->ip();
                if ($res3 = $user->allowField(true)->save($res)){
                    $user->bet($res['tel']);
                    $this->redirect('achieve');
                    exit;
                }else{
                    $this->error($res3);
                }
            }
        }
    }
    public function achieve(){
        return $this->view->fetch();
    }

    public function show(){
        $user = input('user','');

        $res = $this->getshow($user);
        return $this->view->fetch('',$res);
    }

    protected function getshow($user=''){

        $map = [];
        $map2 = [];

        if ($user){
            $map2['platform'] = $map['platform'] = $user;
            $map2['is_reg'] = 0;
        }else{
            return ['gb'=>'','list'=>''];
        }

        $gb = collection(model('apply')->where($map2)->field('count(id) total , FROM_UNIXTIME(create_time,\'%Y-%m-%d\') day ')->order('day desc')->group('day,platform')->select())->toArray();

        $list = model('apply')->order('create_time desc')->where($map)->select();

        return ['gb'=>$gb,'list'=>$list];
    }

    public function trueshow(){
        $user = input('user','');
        $res = $this->getshow($user);
        return $this->view->fetch('',$res);
    }

    public function send()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");

        $user = $this->auth->getUser();

        if(!Validate::regex($mobile, "^1[34578]\d{9}$")){
            $this->error('手机格式错误');
        }
        $event = $event ? $event : 'register';

        $last = Smslib::get($mobile, $event);
        if ($last && time() - $last['createtime'] < 60)

        {
            $this->error(__('发送频繁'));
        }

        if ($event)
        {
            $userinfo = Apply::getByTel($mobile);

            if ( $userinfo )
            {
                //已被注册
//                $this->error(__('已被注册'));
            }
        }

        $ret = Smslib::send($mobile, NULL, $event);
        if ($ret)
        {
            $this->success(__('发送成功'));
        }
        else
        {
            $this->error(__('发送失败'));
        }
    }

    public function yzm(){
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    18,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    false,
            'fontttf' => '6.ttf',
            'codeSet'=>'1234567890'
        ];

        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /**
     * 客户中心
     * */
    public function client(){
        dump(config('site.max'));
//        return $this->view->fetch();

    }

    public function demo(){
        $apply = model('apply');
        $bet = model('applyBet');
        if ( !$apply->bet('15906358516') ){
            dump($apply->getError());
        };
    }
}
